﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections.Concurrent;
using System.Threading;
using System.ServiceModel;
using CRM.Index.Box;
using CRM.Index.IProvider;
using CRM.Entity;
using CRM.Entity.Extra;
using CRM.Entity.Page;

namespace CRM.Index.Provider
{
   
    public partial class ProviderService : IProviderService
    {
       public static ReaderWriterLockSlim  articleType_rws = new ReaderWriterLockSlim();
       
        #region 插入操作
        /// <summary>
        /// 插入操作
        /// </summary>
        /// <param name="ArticleType"></param>
        public bool ArticleType_Insert(ArticleType articleType)
        {
          try
            {
                articleType_rws.EnterWriteLock();
                
                DictionaryBox.articleTypeDictionary.Add(articleType.TypeId, articleType);

                DictionaryBox.articleTypeTypeIdSort.Add(articleType.TypeId);
                
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                
                return false;
            }
            finally
            {
                articleType_rws.ExitWriteLock();
            }
        }
        #endregion

        #region 更新操作
        /// <summary>
        /// 更新操作
        /// </summary>
        /// <param name="ArticleType"></param>
        public bool ArticleType_Update(ArticleType articleType)
        {
          try
            {
                articleType_rws.EnterWriteLock();

                DictionaryBox.articleTypeDictionary[articleType.TypeId] = articleType;
                
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                
                return false;
            }
            finally
            {
                articleType_rws.ExitWriteLock();
            }
        }
        #endregion

        #region 删除操作
        /// <summary>
        /// 删除操作
        /// </summary>
        /// <param name="ArticleType"></param>
        public bool ArticleType_Delete(ArticleType articleType)
        {
          try
            {
                articleType_rws.EnterWriteLock();

                DictionaryBox.articleTypeDictionary.Remove(articleType.TypeId);

                DictionaryBox.articleTypeTypeIdSort.Remove(articleType.TypeId);
                
                return true;
            }  
            catch (Exception e)
            {
                Console.WriteLine(e.Message);

                return false;
            }
            finally
            {
                articleType_rws.ExitWriteLock();
            }
        }
        #endregion
        
        #region 根据主键获取一条
        /// <summary>
        /// 根据主键获取一条
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ArticleType ArticleType_Single(Int64 id)
        {
            try
            {
                articleType_rws.EnterReadLock();

                return DictionaryBox.articleTypeDictionary[id];
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
            finally
            {
                articleType_rws.ExitReadLock();
            }
        }
        #endregion
        
        #region 获取TopK条
        /// <summary>
        /// 获取TopK条
        /// </summary>
        /// <param name="top"></param>
        public ArticleTypePage ArticleType_Top(int top)
        {
            try
            {
                 articleType_rws.EnterReadLock();

                var  articleTypePage = new  ArticleTypePage(){ ListEntity = new List<ArticleType>() };

                var skip = DictionaryBox.articleTypeTypeIdSort.Count - top < 0 ? 0 : DictionaryBox.articleTypeTypeIdSort.Count - top;

                var listID = DictionaryBox.articleTypeTypeIdSort.Skip(skip).Take(top).ToList();

                for (int i = listID.Count - 1; i >= 0; i--)
                {
                    articleTypePage.ListEntity.Add(DictionaryBox.articleTypeDictionary[listID[i]]);
                }

                articleTypePage.RecordCount = DictionaryBox.articleTypeTypeIdSort.Count;

                return articleTypePage;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
            finally
            {
                 articleType_rws.ExitReadLock();
            }
        }
        #endregion

        #region 分页获取条数
        /// <summary>
        /// 分页获取条数
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public ArticleTypePage ArticleType_Page(PageInfo info)
        {
            try
            {
                articleType_rws.EnterReadLock();

                var articleTypePage = new ArticleTypePage() { ListEntity = new List<ArticleType>() };

                var skip = DictionaryBox.articleTypeTypeIdSort.Count - (info.PageIndex * info.PageSize);

                info.PageSize = skip < 0 ? skip + info.PageSize : info.PageSize;

                var listID = DictionaryBox.articleTypeTypeIdSort.Skip(skip).Take(info.PageSize).ToList();

                for (int i = listID.Count - 1; i >= 0; i--)
                {
                    articleTypePage.ListEntity.Add(DictionaryBox.articleTypeDictionary[listID[i]]);
                }

                articleTypePage.RecordCount = DictionaryBox.articleTypeTypeIdSort.Count;

                return articleTypePage;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
            finally
            {
                articleType_rws.ExitReadLock();
            }
        } 
        #endregion
        
        #region 分页获取条数
        /// <summary>
        /// 分页获取条数
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public ArticleTypeExtraPage ArticleTypeExtra_Page(PageInfo info)
        {
            try
            {
                articleType_rws.EnterReadLock();

                var articleTypeExtaPage = new ArticleTypeExtraPage() { ListEntity = new List<ArticleTypeExtra>() };

                var skip = DictionaryBox.articleTypeTypeIdSort.Count - (info.PageIndex * info.PageSize);

                info.PageSize = skip < 0 ? skip + info.PageSize : info.PageSize;

                var listID = DictionaryBox.articleTypeTypeIdSort.Skip(skip).Take(info.PageSize).ToList();

                for (int i = listID.Count - 1; i >= 0; i--)
                {
                    var single = DictionaryBox.articleTypeDictionary[listID[i]];

                    var articleTypeExtra = new ArticleTypeExtra()
                    {
                        TypeId=single.TypeId,
                        TypeName=single.TypeName,
                        CreateTime=single.CreateTime,
                        Disable=single.Disable,
                    };

                    articleTypeExtaPage.ListEntity.Add(articleTypeExtra);
                }

                articleTypeExtaPage.RecordCount = DictionaryBox.articleTypeTypeIdSort.Count;

                return articleTypeExtaPage;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
            finally
            {
                articleType_rws.ExitReadLock();
            }
        }
        #endregion
    }
}
